package com.lazason.wib4android;

import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import weibo4andriod.Comment;
import weibo4andriod.DirectMessage;
import weibo4andriod.Status;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

public class Util {
	public static Map status2Map(Status status) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("profile", status.getUser().getProfileImageURL().toString());
		map.put("weibo", status.getText());
		map.put("username", status.getUser().getName());
		map.put("id", status.getUser().getId());
		map.put("description", status.getUser().getDescription());
		map.put("location", status.getUser().getLocation());
		map.put("weibo_id", status.getId());
		map.put("picture", status.getThumbnail_pic());
		map.put("bmiddle_pic", status.getBmiddle_pic());
		map.put("original_pic", status.getOriginal_pic());
		map.put("in_reply_to_status_id ", status.getInReplyToStatusId());
		map.put("in_reply_to_user_id", status.getInReplyToUserId());
		map.put("in_reply_to_screen_name", status.getInReplyToScreenName());
		map.put("created_at", status.getCreatedAt());
		map.put("comments_count", status.getCommentsCount());
		map.put("repost_count", status.getRepostsCount());
		map.put("created_at", status.getCreatedAt());
		if (status.getRetweetDetails() != null) {
			map.put("retweetedText", status.getRetweetDetails().getText());
			map.put("retweetedOriginalPic", status.getRetweetDetails()
					.getOriginal_pic());
			map.put("retweetedThumbnailPic", status.getRetweetDetails()
					.getThumbnail_pic());
			map.put("retweetedUsername", status.getRetweetDetails()
					.getScreen_name());
			if (status.getRetweetDetails().getScreen_name() == null||"".equals(status.getRetweetDetails().getScreen_name())) {
				map.put("retweetedUsername", status.getRetweetDetails()
						.getRetweetingUser().getScreenName());
			}
			map.put("retweeted_created_at", status.getRetweetDetails()
					.getRetweetedAt());
		}

		return map;
	}

	public static Map comment2Map(Comment comment) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("profile", comment.getUser().getProfileImageURL().toString());
		map.put("comment", comment.getText());
		map.put("username", comment.getUser().getName());
		map.put("created_at", comment.getCreatedAt());
		map.put("id", comment.getUser().getId());
		map.put("in_reply_to_status_id ", comment.getInReplyToStatusId());
		map.put("in_reply_to_user_id", comment.getInReplyToUserId());
		map.put("in_reply_to_screen_name", comment.getInReplyToScreenName());

		if (comment.getRetweetDetails() != null) {
			map.put("retweetedText", comment.getRetweetDetails().getText());
			map.put("retweetedOriginalPic", comment.getRetweetDetails()
					.getOriginal_pic());
			map.put("retweetedThumbnailPic", comment.getRetweetDetails()
					.getThumbnail_pic());
			map.put("retweetedUsername", comment.getRetweetDetails().getUser()
					.getScreenName());
		}

		return map;
	}

	public static Map message2Map(DirectMessage message) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("message", message.getText());
		map.put("created_at", message.getCreatedAt());
		map.put("id", message.getId());
		map.put("sender_id ", message.getSenderId());
		map.put("sender_name", message.getSenderScreenName());
		map.put("sender_profile", message.getSender().getProfileImageURL());

		return map;
	}

	public static String getCreatedAtString(Date createdAt) {
		long currentTime = System.currentTimeMillis();
		DateFormat dateFormat = DateFormat.getDateInstance();
		if (currentTime - createdAt.getTime() < 60000) {
			return (currentTime - createdAt.getTime()) / 1000 + "秒前";
		} else if (currentTime - createdAt.getTime() < 3600000) {
			return (currentTime - createdAt.getTime()) / 60000 + "分前";
		} else if (currentTime - createdAt.getTime() < 86400000) {
			return (currentTime - createdAt.getTime()) / 3600000 + "小时前";
		} else if (currentTime - createdAt.getTime() < 604800000) {
			return (currentTime - createdAt.getTime()) / 86400000 + "天前";
		} else {
			return dateFormat.format(createdAt);
		}

	}

	public static void setMode4View(Context context, View v) {
		if ("night".equals(MyApplication.style)) {
			v.setBackgroundResource(R.color.black);

		} else {
			v.setBackgroundResource(R.color.white);
		}
		if (v instanceof TextView) {
			if ("night".equals(MyApplication.style)) {
				((TextView) v).setTextColor(context.getResources().getColor(
						R.color.dark_gray));

			} else {
				((TextView) v).setTextColor(context.getResources().getColor(
						R.color.dim_gray));

			}
		}
	}

}
